* Development (Night Lights)
	
clear all
set more off
pause on
set trace off
cap log close
set matsize 5000
set maxvar 32767
	
loc dir "/Users/mateo/Desktop/"
	
	cd "/Users/mateo/Desktop/"
	
		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear
		
			
		foreach x in 1995 {
		
			preserve
			
				keep if Año<=`x'
							
				collapse (mean)  spatlagguerrsum100_100, by(codmpio)
				
				ren (spatlagguerrsum100_100)=_`x'
				
				tempfile neighbor
				save `neighbor', replace
			
			
			restore
			
			merge m:1 codmpio using `neighbor'
			drop _m
		
		
		}
		
		** Controls X year
			loc controlsXyear
			loc x
			
			foreach control of loc controls {
			
				loc x c.`control'##c.Año
				
				loc controlsXyear `controlsXyear' `x' 

			}
			
		
			by codmpio (Año), sort: gen first_title = sum(landtitle) == 1
			gen first_title_year=Año if first_title==1
			
			bys codmpio (first_title_year): replace first_title_year= first_title_year[_n-1] if mi(first_title_year) 
			
			gen chyears=Año - first_title_year
			replace chyears=-10 if chyears<=-10
			replace chyears=10 if chyears>=10
			
			gen title_adopted=0 if first_title_year > Año
			replace title_adopted = 1 if first_title_year<=Año
			replace title_adopted =. if mi(first_title_year)
			egen all_attacks=rowtotal(sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)
			
			lab var all_attacks 				"All attacks"
			lab var sumattacks_public 			"Police & Army attacks"
			lab var sumattacks_paramilitary 	"Paramilitary attacks"
			lab var sumattacks_guerrilla 		"Guerrilla attacks"
				
			gen as_night_light=asinh(light_mean)	
			
			for any low med high: gen X=.
			xi : areg  as_night_light    Año##coddepto i.chyears , r cluster(codmpio) absorb(codmpio)
			for Y in num 2/10 11/20: replace med=_b[_Ichyears_Y] if _Ichyears_Y==1 
			for Y in num 2/10 11/20: replace low=_b[_Ichyears_Y]-1.64*_se[_Ichyears_Y] if _Ichyears_Y==1  //Note:10% C.I.
			for Y in num 2/10 11/20: replace high=_b[_Ichyears_Y]+1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 //Note:10% C.I.
			replace med=0 if chyears==0 
			replace high=0+1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
			replace low=0-1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
			
			for Z in var low med high: replace Z=Z-(_b[_Ichyears_2]+_b[_Ichyears_3]+_b[_Ichyears_4]+_b[_Ichyears_5]+_b[_Ichyears_6] + _b[_Ichyears_7] + _b[_Ichyears_8] + _b[_Ichyears_9] + _b[_Ichyears_10] +0)/10 
			for Z in var low med high: replace Z=0 if chyears==0 
			twoway (line low chyears, sort blcolor(blue) clpat(shortdash) clwidth(thin)) (line high chyears, sort clcolor(blue) clpat(shortdash) clwidth(thin)) (connected med chyears, color(black) sort), ytitle("% Change") ylabel(-0.6 "-60%" -0.4 "-40%" -0.2 "-20%"  0 "0%"  .2 "20%" .4 "40%" .6 "60%" .8 "80%"   1 "100%" 1.2 "120%", angle(horizontal)) yline(0, lcolor(black)) xtitle(Years Since/Until First Land Title Assigned) xlabel(-10(2)10 -10 "<-10 yrs" 10 ">10 yrs") xline(0, lcolor(black)) title("{bf:Night Light}", size(vlarge)) legend(order(2 "Coefficient Estimates" 1 "10% C.I.") off) xsize(5) ysize(3.5) name(night_light, replace) graphregion(color(white))
				
			graph export `"/Users/mateo/Desktop/nightlight_phasein.png"', replace
			
			
		* Mining 
		
		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear
		
	
		foreach x in 1995 {
		
			preserve
			
				keep if Año<=`x'
							
				collapse (mean)  spatlagguerrsum100_100, by(codmpio)
				
				ren (spatlagguerrsum100_100)=_`x'
				
				tempfile neighbor
				save `neighbor', replace
			
			
			restore
			
			merge m:1 codmpio using `neighbor'
			drop _m
		
		
		}
		
		** Controls X year
			loc controlsXyear
			loc x
			
			foreach control of loc controls {
			
				loc x c.`control'##c.Año
				
				loc controlsXyear `controlsXyear' `x' 

			}
			
		
		by codmpio (Año), sort: gen first_title = sum(landtitle) == 1
		gen first_title_year=Año if first_title==1
		
		bys codmpio (first_title_year): replace first_title_year= first_title_year[_n-1] if mi(first_title_year) 
		
		gen chyears=Año - first_title_year
		replace chyears=-10 if chyears<=-10
		replace chyears=10 if chyears>=10
		
		gen title_adopted=0 if first_title_year > Año
		replace title_adopted = 1 if first_title_year<=Año
		replace title_adopted =. if mi(first_title_year)
		egen all_attacks=rowtotal(sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)
		
		lab var all_attacks 				"All attacks"
		lab var sumattacks_public 			"Police & Army attacks"
		lab var sumattacks_paramilitary 	"Paramilitary attacks"
		lab var sumattacks_guerrilla 		"Guerrilla attacks"
		
		gen as_mining=asinh(hectareas_titulos_mineros )	
		
		for any low med high: gen X=.
		xi : areg  as_mining  Año##coddepto  c.disbogota##i.Año i.chyears, r cluster(codmpio) absorb(codmpio)
		for Y in num 2/10 11/20: replace med=_b[_Ichyears_Y] if _Ichyears_Y==1 
		for Y in num 2/10 11/20: replace low=_b[_Ichyears_Y]-1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 		//Note:10% C.I.
		for Y in num 2/10 11/20: replace high=_b[_Ichyears_Y]+1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 //Note:10% C.I.
		replace med=0 if chyears==0 
		replace high=0+1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
		replace low=0-1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
		for Z in var low med high: replace Z=Z-(_b[_Ichyears_2]+_b[_Ichyears_3]+_b[_Ichyears_4]+_b[_Ichyears_5]+_b[_Ichyears_6] + _b[_Ichyears_7] + _b[_Ichyears_8] + _b[_Ichyears_9] + _b[_Ichyears_10] +0)/10 
		for Z in var low med high: replace Z=0 if chyears==0 
		twoway (line low chyears, sort blcolor(blue) clpat(shortdash) clwidth(thin)) (line high chyears, sort clcolor(blue) clpat(shortdash) clwidth(thin)) (connected med chyears, color(black) sort), ytitle("% Change") /*ylabel(-0.6 "-60%" -0.4 "-40%" -0.2 "-20%"  0 "0%"  .2 "20%" .4 "40%" .6 "60%" .8 "80%"   1 "100%" 1.2 "120%", angle(horizontal))*/ yline(0, lcolor(black)) xtitle(Years Since/Until First Land Title Assigned) xlabel(-10(2)10 -10 "<-10 yrs" 10 ">10 yrs") xline(0, lcolor(black)) title("{bf:Mining}", size(vlarge)) legend(order(2 "Coefficient Estimates" 1 "10% C.I.") off) xsize(5) ysize(3.5) name(as_mining, replace) graphregion(color(white))

		graph export `"/Users/mateo/Desktop/minning_phasein.png"', replace
		
		
		* Oil & Gas
	
		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear
		
		
		foreach x in 1995 {
		
			preserve
			
				keep if Año<=`x'
							
				collapse (mean)  spatlagguerrsum100_100, by(codmpio)
				
				ren (spatlagguerrsum100_100)=_`x'
				
				tempfile neighbor
				save `neighbor', replace
			
			
			restore
			
			merge m:1 codmpio using `neighbor'
			drop _m
		
		
		}
		
		** Controls X year
			loc controlsXyear
			loc x
			
			foreach control of loc controls {
			
				loc x c.`control'##c.Año
				
				loc controlsXyear `controlsXyear' `x' 

			}
			
		
		by codmpio (Año), sort: gen first_title = sum(landtitle) == 1
		gen first_title_year=Año if first_title==1
		
		bys codmpio (first_title_year): replace first_title_year= first_title_year[_n-1] if mi(first_title_year) 
		
		gen chyears=Año - first_title_year
		replace chyears=-10 if chyears<=-10
		replace chyears=10 if chyears>=10
		
		gen title_adopted=0 if first_title_year > Año
		replace title_adopted = 1 if first_title_year<=Año
		replace title_adopted =. if mi(first_title_year)
		egen all_attacks=rowtotal(sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)
		
		lab var all_attacks 				"All attacks"
		lab var sumattacks_public 			"Police & Army attacks"
		lab var sumattacks_paramilitary 	"Paramilitary attacks"
		lab var sumattacks_guerrilla 		"Guerrilla attacks"
		
		gen as_oil_gas=asinh(hectareas_oil_gas )	
		
		for any low med high: gen X=.
		xi : areg  as_oil_gas   Año##coddepto i.chyears, r cluster(codmpio) absorb(codmpio)
		for Y in num 2/10 11/20: replace med=_b[_Ichyears_Y] if _Ichyears_Y==1 
		for Y in num 2/10 11/20: replace low=_b[_Ichyears_Y]-1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 		//Note:10% C.I.
		for Y in num 2/10 11/20: replace high=_b[_Ichyears_Y]+1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 //Note:10% C.I.
		replace med=0 if chyears==0 
		replace high=0+1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
		replace low=0-1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
		
		for Z in var low med high: replace Z=Z-(_b[_Ichyears_2]+_b[_Ichyears_3]+_b[_Ichyears_4]+_b[_Ichyears_5]+_b[_Ichyears_6] + _b[_Ichyears_7] + _b[_Ichyears_8] + _b[_Ichyears_9] + _b[_Ichyears_10] +0)/10 
		for Z in var low med high: replace Z=0 if chyears==0 
		twoway (line low chyears, sort blcolor(blue) clpat(shortdash) clwidth(thin)) (line high chyears, sort clcolor(blue) clpat(shortdash) clwidth(thin)) (connected med chyears, color(black) sort), ytitle("% Change") ylabel(-0.6 "-60%" -0.4 "-40%" -0.2 "-20%"  0 "0%"  .2 "20%" .4 "40%" .6 "60%" .8 "80%"   1 "100%" 1.2 "120%", angle(horizontal)) yline(0, lcolor(black)) xtitle(Years Since/Until First Land Title Assigned) xlabel(-10(2)10 -10 "<-10 yrs" 10 ">10 yrs") xline(0, lcolor(black)) title("{bf:Oil & Gas}", size(vlarge)) legend(order(2 "Coefficient Estimates" 1 "10% C.I.") off) xsize(5) ysize(3.5) name(as_oil_gas, replace) graphregion(color(white))

		graph export `"/Users/mateo/Desktop/extractive_phasein.png"', replace
	
	* PIB Agricola (agricultural GDP)  per capita
	
		use "/Users/mateo/Desktop/master_violence_landtitle_caracteristicas.dta", clear
		
		
		foreach x in 1995 {
		
			preserve
			
				keep if Año<=`x'
							
				collapse (mean)  spatlagguerrsum100_100, by(codmpio)
				
				ren (spatlagguerrsum100_100)=_`x'
				
				tempfile neighbor
				save `neighbor', replace
			
			
			restore
			
			merge m:1 codmpio using `neighbor'
			drop _m
		
		
		}
		
		** Controls X year
			loc controlsXyear
			loc x
			
			foreach control of loc controls {
			
				loc x c.`control'##c.Año
				
				loc controlsXyear `controlsXyear' `x' 

			}
			
		
		by codmpio (Año), sort: gen first_title = sum(landtitle) == 1
		gen first_title_year=Año if first_title==1
		
		bys codmpio (first_title_year): replace first_title_year= first_title_year[_n-1] if mi(first_title_year) 
		
		gen chyears=Año - first_title_year
		replace chyears=-10 if chyears<=-10
		replace chyears=10 if chyears>=10
		
		gen title_adopted=0 if first_title_year > Año
		replace title_adopted = 1 if first_title_year<=Año
		replace title_adopted =. if mi(first_title_year)
		egen all_attacks=rowtotal(sumattacks_public sumattacks_guerrilla sumattacks_paramilitary)
		
		lab var all_attacks 				"All attacks"
		lab var sumattacks_public 			"Police & Army attacks"
		lab var sumattacks_paramilitary 	"Paramilitary attacks"
		lab var sumattacks_guerrilla 		"Guerrilla attacks"
		gen pib_agricola_pc=(pib_agricola/pop95)
				
		for any low med high: gen X=.
		xi : areg  pib_agricola_pc     Año##coddepto i.chyears, r cluster(codmpio) absorb(codmpio)
		for Y in num 2/10 11/20: replace med=_b[_Ichyears_Y] if _Ichyears_Y==1 
		for Y in num 2/10 11/20: replace low=_b[_Ichyears_Y]-1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 		//Note: 10% C.I.
		for Y in num 2/10 11/20: replace high=_b[_Ichyears_Y]+1.64*_se[_Ichyears_Y] if _Ichyears_Y==1 //Note: 10% C.I.
		replace med=0 if chyears==0 
		replace high=0+1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
		replace low=0-1*(_se[_Ichyears_10]+_se[_Ichyears_11])/2 if chyears==0 
		for Z in var low med high: replace Z=Z-(_b[_Ichyears_2]+_b[_Ichyears_3]+_b[_Ichyears_4]+_b[_Ichyears_5]+_b[_Ichyears_6] + _b[_Ichyears_7] + _b[_Ichyears_8] + _b[_Ichyears_9] + _b[_Ichyears_10] +0)/10 
		for Z in var low med high: replace Z=0 if chyears==0 
		twoway (line low chyears, sort blcolor(blue) clpat(shortdash) clwidth(thin)) (line high chyears, sort clcolor(blue) clpat(shortdash) clwidth(thin)) (connected med chyears, color(black) sort), ytitle("% Change") ylabel(-0.6 "-60%" -0.4 "-40%" -0.2 "-20%"  0 "0%"  .2 "20%" .4 "40%" .6 "60%" .8 "80%"   1 "100%" 1.2 "120%", angle(horizontal)) yline(0, lcolor(black)) xtitle(Years Since/Until First Land Title Assigned) xlabel(-10(2)10 -10 "<-10 yrs" 10 ">10 yrs") xline(0, lcolor(black)) title("{bf:Agricultural GDP per capita}", size(vlarge)) legend(order(2 "Coefficient Estimates" 1 "10% C.I.") off) xsize(5) ysize(3.5) name(pib_agricola_pc, replace) graphregion(color(white))
	
		graph export `"/Users/mateo/Desktop/pib_agricola_phasein.png"', replace

	
//	graph combine as_night_light as_mining as_oil_gas pib_agricola_pc, ///
//		saving(`"/Users/mateo/Desktop/Econ_performance_phasein.png"', replace) ycommon cols(2)  graphregion(color(white))  altshrink
